package com.sugon.modules.alarm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.sugon.dao.AlarmCodeBaseDao;
import com.sugon.entity.AlarmCodeBaseEntity;
import com.sugon.modules.alarm.service.AlarmCodeBaseService;
import com.sugon.utils.Constant;
import com.sugon.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 设备报警代码库Service实现类
 *
 * @author sugon
 * @date 2023-02-09 08:39:17
 */
@Service("alarmCodeBaseService")
public class AlarmCodeBaseServiceImpl implements AlarmCodeBaseService {
    @Autowired
    private AlarmCodeBaseDao alarmCodeBaseDao;

    @Override
    public AlarmCodeBaseEntity query(String faultCategoryId, String devId, String alarmCode) {
        AlarmCodeBaseEntity queryParam = new AlarmCodeBaseEntity();
        // 设备ID和报警代码必填
        queryParam.setDevId(devId);
        // 报警代码必填
        if (StrUtil.isNotEmpty(alarmCode)) {
            queryParam.setAlarmCode(alarmCode);
        }
        // 报警所属ccc分类非必填
        if (StrUtil.isNotEmpty(faultCategoryId)) {
            queryParam.setAlarmCategoryId(faultCategoryId);
        }
        List<AlarmCodeBaseEntity> matchedList = queryList(BeanUtil.beanToMap(queryParam));
        return CollectionUtil.isNotEmpty(matchedList) ? matchedList.get(0) : null;
    }

    @Override
    public List<AlarmCodeBaseEntity> queryList(String alarmCategoryId, String devId, String alarmCode) {
        AlarmCodeBaseEntity queryParam = new AlarmCodeBaseEntity();
        queryParam.setDevId(devId);
        // 拼接报警分类ID
        if (StrUtil.isNotEmpty(alarmCategoryId)) {
            queryParam.setAlarmCategoryId(alarmCategoryId);
        }
        // 拼接报警代码
        if (StrUtil.isNotEmpty(alarmCode)) {
            queryParam.setAlarmCode(alarmCode);
        }
        List<AlarmCodeBaseEntity> matchedList = queryList(BeanUtil.beanToMap(queryParam));
        return ObjectUtil.defaultIfNull(matchedList, CollectionUtil.newArrayList());
    }

    @Override
    public AlarmCodeBaseEntity queryOrCreateIfNotExists(String faultCategoryId, String devId, String alarmCode, String alarmDesc, boolean isCreate) {
        AlarmCodeBaseEntity alarmCodeBase = query(faultCategoryId, devId, alarmCode);
        if (alarmCodeBase == null) {
            if (isCreate) {
                alarmCodeBase = new AlarmCodeBaseEntity(faultCategoryId, devId, alarmCode, alarmDesc);
                // 设置为待审核，需要由各个车间审核
                alarmCodeBase.setState("1");
                save(alarmCodeBase);
            }
        }
        return alarmCodeBase;
    }

    @Override
    public AlarmCodeBaseEntity queryObject(String id) {
        return alarmCodeBaseDao.queryObject(id);
    }

    @Override
    public List<AlarmCodeBaseEntity> queryList(Map<String, Object> map) {
        return alarmCodeBaseDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return alarmCodeBaseDao.queryTotal(map);
    }

    @Override
    public int save(AlarmCodeBaseEntity alarmCodeBase) {
        alarmCodeBase.setId(RandomUtil.randomString(32));
        alarmCodeBase.setCreateTime(DateUtil.date());
        alarmCodeBase.setCreateBy(ShiroUtils.getUserName());
        alarmCodeBase.setDelFlag(Constant.IS_NOT_DEL_INT);
        return alarmCodeBaseDao.save(alarmCodeBase);
    }

    @Override
    public int update(AlarmCodeBaseEntity alarmCodeBase) {
        alarmCodeBase.setUpdateTime(DateUtil.date());
        alarmCodeBase.setUpdateBy(ShiroUtils.getUserName());
        return alarmCodeBaseDao.update(alarmCodeBase);
    }

    @Override
    public int delete(String id) {
        return alarmCodeBaseDao.delete(id);
    }

    @Override
    public int deleteBatch(String[] ids) {
        return alarmCodeBaseDao.deleteBatch(ids);
    }
}
